/**
 * department控制器
 *
 * @author sxf
 * @email sxf02615@163.com
 * @date 2025/1/15
 */
package com.sxf.crm.controller;

import com.sxf.crm.dto.DepartmentDTO;
import com.sxf.crm.dto.UserDTO;
import com.sxf.crm.entity.Department;
import com.sxf.crm.service.DepartmentService;
import com.sxf.crm.utils.SecurityUtils;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PutMapping;


@RestController
@RequestMapping("/api/departments")
public class DepartmentController {

    private final DepartmentService departmentService;

    public DepartmentController(DepartmentService departmentService) {
        this.departmentService = departmentService;
    }

    @GetMapping
    @PreAuthorize("hasAuthority('department:view') or principal.isAdmin")
    public List<DepartmentDTO> getAllDepartments(@RequestParam(required = false) String name) {
        return departmentService.getAllDepartmentsAsDTO(name);
    }

    @GetMapping("/{id}/users")
    @PreAuthorize("hasAuthority('department:view') or principal.isAdmin")
    public List<UserDTO> getUsersByDepartmentAndChildren(@PathVariable Long id) {
        return departmentService.getUsersByDepartmentAndChildren(id);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('department:delete') or principal.isAdmin")
    public void deleteDepartment(@PathVariable Long id) {
        departmentService.deleteDepartment(id);
    }

    @PostMapping()
    @PreAuthorize("hasAuthority('department:create') or principal.isAdmin")
    public DepartmentDTO createDepartment(@RequestBody DepartmentDTO departmentDTO) {
        Long currentUserId = SecurityUtils.getCurrentUserId();
        
        Department department = new Department();
        department.setName(departmentDTO.getName());
        department.setDescription(departmentDTO.getDescription());
        department.setParent(departmentService.findById(departmentDTO.getParentId()));
        department.setCreateAt(System.currentTimeMillis());
        department.setCreateUserId(currentUserId);

        return DepartmentDTO.fromEntity(departmentService.saveDepartment(department));
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAuthority('department:edit') or principal.isAdmin")
    public DepartmentDTO updateDepartment(@PathVariable Long id, @RequestBody DepartmentDTO departmentDTO) {
        Department department = departmentService.findById(id);
        department.setName(departmentDTO.getName());
        department.setDescription(departmentDTO.getDescription());
        department.setParent(departmentService.findById(departmentDTO.getParentId()));
        department.setUpdateAt(System.currentTimeMillis());
        department.setUpdateUserId(SecurityUtils.getCurrentUserId());
        return DepartmentDTO.fromEntity(departmentService.saveDepartment(department));
    }
}
